package com.huilai.platform.country_platform.repository;

import com.huilai.platform.country_platform.model.PartyActivities;
import com.huilai.platform.country_platform.model.User;
import com.huilai.platform.country_platform.model.VillageCommittee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;

/**
 * 作者：洪霓
 * 日期：2023/3/4
 * 注释：
 */
public interface VillageCommitteeReposity extends JpaRepository<VillageCommittee, Integer>, JpaSpecificationExecutor<VillageCommittee> {
    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = "insert into village_committee(name, sex, unit, position, telephone, work, introduction, image)" +
            "values(:#{#villageCommittee.name},:#{#villageCommittee.sex},:#{#villageCommittee.unit},:#{#villageCommittee.position},:#{#villageCommittee.telephone},:#{#villageCommittee.work},:#{#villageCommittee.introduction},:#{#villageCommittee.image})", nativeQuery = true)
    Integer addVillageCommittee(VillageCommittee villageCommittee);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = "update village_committee vc set vc.name = :#{#villageCommittee.name}, " +
            "vc.sex = :#{#villageCommittee.sex}, " +
            "vc.unit = :#{#villageCommittee.unit}, " +
            "vc.position = :#{#villageCommittee.position}, " +
            "vc.telephone = :#{#villageCommittee.telephone}, " +
            "vc.work = :#{#villageCommittee.work}, " +
            "vc.introduction = :#{#villageCommittee.introduction}, " +
            "vc.image = :#{#villageCommittee.image} where vc.id = :#{#villageCommittee.id}",nativeQuery = true)
    Integer updateCommittee(VillageCommittee villageCommittee);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query("delete from VillageCommittee vc where vc.id = :id")
    Integer deleteCommitteeById(Integer id);

    VillageCommittee findVillageCommitteeByPosition(String position);
}
